using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace MemoryGame
{
    public class StateMainMenu : DEF
    {
        public static Texture2D splashBitmap = null;
        public static Texture2D gameTitleBitmap = null;

        public const int MENU_PLAY = 0;
        public const int MENU_HOW_PLAY = 1;

        public const int MENU_CREDIT = 2;
        public const int MENU_EXIT = 3;


        public static int[] arrayMenu = { MENU_PLAY, MENU_HOW_PLAY, MENU_CREDIT, MENU_EXIT };
        public static String[] arrayMenuString = { "PLAY", "HOW TO PLAY", "ABOUT", "EXIT" };
        public static int MENU_BEGIN_X = 0; //will iit in contructor
        public static int MENU_BEGIN_Y = 200;
        public static int MENU_ELEMENT_W = 0; //will iit in contructor
        public static int MENU_ELEMENT_H = 0; //will iit in contructor
        public static int MENU_ELEMENT_SPACE = 20;
        public static int MENU_H = SCREEN_HEIGHT;
        public static int MENU_BUTTON_ICON_Y = SCREEN_HEIGHT;

        public static void SendMessage(int type)
        {
            switch (type)
            {
                case MESSAGE_CTOR:
                    splashBitmap = Game1.contentManager.Load<Texture2D>("image//splash_1280x800");
                    gameTitleBitmap = Game1.contentManager.Load<Texture2D>("image//gametitle");
                    MENU_ELEMENT_H = (int)(texttureButtonMenu[DEF.FRAME_BUTTON_HIGHTLIGHT].Height * Game1.SCALE_Y);
                    MENU_ELEMENT_W = (int)(texttureButtonMenu[DEF.FRAME_BUTTON_HIGHTLIGHT].Width * Game1.SCALE_X);
                    MENU_BEGIN_X = SCREEN_WIDTH / 2 - MENU_ELEMENT_W / 2;
                    MENU_ELEMENT_SPACE = MENU_ELEMENT_H / 2;//(SCREEN_HEIGHT -((arrayMenu.length + 3)*MENU_ELEMENT_H)) /arrayMenu.length;
                    if (SCREEN_HEIGHT < 400)
                        MENU_ELEMENT_SPACE = MENU_ELEMENT_SPACE / 4;
                    if (MENU_ELEMENT_SPACE < 3)
                        MENU_ELEMENT_SPACE = 3;
                    MENU_H = (MENU_ELEMENT_SPACE + MENU_ELEMENT_H) * arrayMenu.Length;
                    MENU_BEGIN_Y = (SCREEN_HEIGHT - MENU_H) / 2 + 3 * MENU_ELEMENT_H / 2;
                    MENU_BUTTON_ICON_Y = MENU_BEGIN_Y + MENU_H / 2 + 2 * DEF.DIALOG_BUTTON_CONFIRM_H;
                    if (SCREEN_HEIGHT < 400)
                        MENU_BUTTON_ICON_Y = MENU_BEGIN_Y + MENU_H / 2 + DEF.DIALOG_BUTTON_CONFIRM_H + 2 * MENU_ELEMENT_SPACE;
                    StateGameplay.isIngame = false;
                    if (Game1.mPreState != Game1.STATE_CREADIT && Game1.mPreState != Game1.STATE_HOW_TO_PLAY && Game1.mPreState != Game1.STATE_SELECT_LEVEL)
                        SoundManager.playsoundLoop(0, true);

                   
                    break;
                case MESSAGE_UPDATE:
                  
                    int temp = -1;
                    if (Dialog.isShowDialog)
                    {
                        Dialog.updateDialog();
                        if (InputState.isBackKeyPress())
                            Dialog.hideDialog();
                        return;
                    }
                    if (InputState.isBackKeyPress())
                    {
                        Dialog.showDialog(0, "EXIT???", "DO YOU WANT TO EXIT?", 0);
                        return;
                    }
                    for (int i = 0; i < arrayMenu.Length; i++)
                    {
                        int y = (int)(MENU_BEGIN_Y + i * (MENU_ELEMENT_H + MENU_ELEMENT_SPACE));
                        if (InputState.isTouchReleaseInRect(MENU_BEGIN_X, y, MENU_ELEMENT_W, MENU_ELEMENT_H))
                        {

                            //  if (i != MENU_OPTION_SOUND)
                            SoundManager.playSound(SoundManager.SOUND_SELECT);
                            switch (i)
                            {
                                case MENU_PLAY:
                                    Game1.changeState(Game1.STATE_SELECT_MODE);
                                    break;
                                case MENU_HOW_PLAY:
                                    Game1.changeState(Game1.STATE_HOW_TO_PLAY);
                                    break;
                                case MENU_CREDIT:
                                    Game1.changeState(Game1.STATE_CREADIT);
                                    break;
                                case MENU_EXIT:
                                    Dialog.showDialog(Dialog.DIALOG_TYPE_CONFIRM, "", "DO YOU WANT TO EXIT???", Dialog.DIALOG_NEXTSTATE_EXIT);
                                    break;
                                default:
                                    break;
                            }
                        }
                    }


                    break;
                case MESSAGE_PAINT:

                    //  Game1.spriteBatch.Draw(splashBitmap, new Vector2(0, 0), Color.White);
                    //  Game1.spriteBatch.Draw(gameTitleBitmap, new Vector2(0, 0), Color.White);
                    Game1.spriteBatch.Draw(splashBitmap, new Vector2(0, 0), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                  
                    Game1.spriteBatch.Draw(gameTitleBitmap, new Vector2((SCREEN_WIDTH - gameTitleBitmap.Width * Game1.SCALE_X) / 2, 0), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                    if (Dialog.isShowDialog)
                    {
                        Dialog.drawDialog(Game1.spriteBatch);
                        return;
                    }
                    /*
                     * Draw(Texture2D texture, Vector2 position, Rectangle? source, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float depth)
                     * 
                    *
                     */

                    string text = " ";
                    for (int i = 0; i < arrayMenu.Length; i++)
                    {
                        int y = (int)(MENU_BEGIN_Y + i * (MENU_ELEMENT_H + MENU_ELEMENT_SPACE));
                        if (InputState.isTouchDrapInRect(MENU_BEGIN_X, y, MENU_ELEMENT_W, MENU_ELEMENT_H))
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_HIGHTLIGHT], new Vector2(MENU_BEGIN_X, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);
                        else
                            Game1.spriteBatch.Draw(texttureButtonMenu[DEF.FRAME_BUTTON_NORMAL], new Vector2(MENU_BEGIN_X, y), null, Color.White, 0f, new Vector2(0, 0), new Vector2(Game1.SCALE_X, Game1.SCALE_Y), SpriteEffects.None, 1);

                        text = arrayMenuString[i];
                        Vector2 tempMeasure = Game1.fontNormal.MeasureString(text);
                        Game1.spriteBatch.DrawString(Game1.fontNormal, text, new Vector2(MENU_BEGIN_X + (MENU_ELEMENT_W - tempMeasure.X) / 2, y + (MENU_ELEMENT_H - tempMeasure.Y) / 2), Color.White);

                        //    fontsmall_White.drawString(mainCanvas, arrayMenuString[i], MENU_BEGIN_X, y - fontsmall_White.getFontHeight()/2, BitmapFont.ALIGN_CENTER);
                    }

                    //if(System.currentTimeMillis()%1000 > 500)
                    //if (fontbig_White != null)
                    //	fontbig_White.drawString(mainCanvas, "TOUCH THE SCREEN TO CONTINUE", SCREEN_WIDTH/2, SCREEN_HEIGHT - 150, BitmapFont.ALIGN_CENTER);	

                    break;
                case MESSAGE_DTOR:
                    break;
            }
        }
    }
}
